package com.oncallmng.api;

import com.oncallmng.common.PageParam;
import com.oncallmng.common.PageResult;
import com.oncallmng.common.Result;
import com.oncallmng.model.Alert;
import com.oncallmng.model.EventLog;
import com.oncallmng.service.AlertService;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * 告警控制器
 */
@RestController
@RequestMapping("/alerts")
@RequiredArgsConstructor
public class AlertController {
    
    private final AlertService alertService;
    
    /**
     * 根据ID查询告警
     * @param id 告警ID
     * @return 告警信息
     */
    @GetMapping("/{id}")
    public Result<Alert> getById(@PathVariable String id) {
        return Result.success(alertService.getById(id));
    }
    
    /**
     * 分页查询告警列表
     * @param pageParam 分页参数
     * @param teamId 团队ID
     * @param status 状态
     * @param severity 级别
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 告警列表
     */
    @GetMapping
    public Result<PageResult<Alert>> page(
            PageParam pageParam,
            @RequestParam String teamId,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String severity,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {
        return Result.success(alertService.page(pageParam, teamId, status, severity, startDate, endDate));
    }
    
    /**
     * 确认告警
     * @param id 告警ID
     * @param request 请求参数
     * @return 结果
     */
    @PostMapping("/{id}/acknowledge")
    @PreAuthorize("hasRole('ONCALL')")
    public Result<Void> acknowledge(@PathVariable String id, @RequestBody Map<String, String> request) {
        String userId = request.get("userId");
        String comment = request.get("comment");
        alertService.acknowledge(id, userId, comment);
        return Result.success(null);
    }
    
    /**
     * 解决告警
     * @param id 告警ID
     * @param request 请求参数
     * @return 结果
     */
    @PostMapping("/{id}/resolve")
    @PreAuthorize("hasRole('ONCALL')")
    public Result<Void> resolve(@PathVariable String id, @RequestBody Map<String, String> request) {
        String userId = request.get("userId");
        String comment = request.get("comment");
        alertService.resolve(id, userId, comment);
        return Result.success(null);
    }
    
    /**
     * 转派告警
     * @param id 告警ID
     * @param request 请求参数
     * @return 结果
     */
    @PostMapping("/{id}/assign")
    @PreAuthorize("hasRole('ONCALL')")
    public Result<Void> assign(@PathVariable String id, @RequestBody Map<String, String> request) {
        String userId = request.get("userId");
        String assignToId = request.get("assignToId");
        String comment = request.get("comment");
        alertService.assign(id, userId, assignToId, comment);
        return Result.success(null);
    }
    
    /**
     * 添加评论
     * @param id 告警ID
     * @param request 请求参数
     * @return 结果
     */
    @PostMapping("/{id}/comments")
    @PreAuthorize("hasRole('ONCALL')")
    public Result<Void> addComment(@PathVariable String id, @RequestBody Map<String, String> request) {
        String userId = request.get("userId");
        String comment = request.get("comment");
        alertService.addComment(id, userId, comment);
        return Result.success(null);
    }
    
    /**
     * 获取告警处理日志
     * @param id 告警ID
     * @return 处理日志列表
     */
    @GetMapping("/{id}/logs")
    public Result<List<EventLog>> getEventLogs(@PathVariable String id) {
        return Result.success(alertService.getEventLogs(id));
    }
    
    /**
     * 获取告警统计信息
     * @param teamId 团队ID
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 统计信息
     */
    @GetMapping("/statistics")
    public Result<Map<String, Object>> getStatistics(
            @RequestParam String teamId,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {
        return Result.success(alertService.getStatistics(teamId, startDate, endDate));
    }
} 